{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "suitable-romania",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install pyexecjs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "pending-opposition",
   "metadata": {},
   "outputs": [],
   "source": [
    "import execjs\n",
    "import time\n",
    "import requests\n",
    "import json\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "polyphonic-washer",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_js():\n",
    "    f = open(\"./md5.js\", 'r', encoding='utf-8') # 打开JS文件\n",
    "    line = f.readline()\n",
    "    htmlstr = ''\n",
    "    while line:\n",
    "        htmlstr = htmlstr+line\n",
    "        line = f.readline()\n",
    "    return htmlstr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "incorporate-paint",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_sign(url,t):\n",
    "    data = \"8tj7n723px2z06o6fuf\"+url+t\n",
    "    js_str = get_js()\n",
    "    ctx = execjs.compile(js_str) #加载JS文件\n",
    "    return (ctx.call('r', data))  #调用js方法  第一个参数是JS的方法名，后面的data和key是js方法的参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ordered-soldier",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_t():\n",
    "    res = str(time.time()).split('.')[0]\n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "technological-workstation",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_get_url(url_no_wenhao):\n",
    "    # /bookmanage/wordplus/010010101/50.txt\n",
    "    \n",
    "    url_temp = url_no_wenhao\n",
    "    t = get_t()\n",
    "    sign = get_sign(url_temp,t)\n",
    "    \n",
    "    res = url_temp+\"?\"+\"sign=\"+sign+\"&t=\"+t\n",
    "    \n",
    "    pre = 'https://ksedusit-1302986692.file.myqcloud.com'    \n",
    "    return pre+res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "approved-health",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_text_from_url(url_1):\n",
    "    get_url = get_get_url(url_1)\n",
    "    get_headers = {\n",
    "        'Host': 'ksedusit-1302986692.file.myqcloud.com',\n",
    "        'Connection': 'keep-alive',\n",
    "        'Accept': '*/*',\n",
    "        'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.2(0x18000226) NetType/WIFI Language/zh_CN',\n",
    "        'Accept-Language': 'zh-cn',\n",
    "        'Referer': 'https://servicewechat.com/wx20f32710680b5231/0/page-frame.html',\n",
    "        'Accept-Encoding': 'gzip, deflate, br'\n",
    "    }\n",
    "    \n",
    "    res = requests.get(url=get_url, headers=get_headers)\n",
    "    \n",
    "    zw_utf8 = res.text.encode(res.encoding).decode('utf-8')\n",
    "    \n",
    "    return zw_utf8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aging-collaboration",
   "metadata": {},
   "outputs": [],
   "source": [
    "# start_url = \"/bookmanage/wordplus/010010101/1.txt\"\n",
    "\n",
    "# end_url = \"/bookmanage/wordplus/010010101/4737.txt\"\n",
    "\n",
    "# data = get_text_from_url(end_url)\n",
    "\n",
    "# f2 = open('new_json.json','w',encoding='utf-8')\n",
    "# f2.write(data)\n",
    "# f2.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "representative-democrat",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(1,4738):\n",
    "    print((\"\\r\"+\"#\"*2+\"进行第:{}条，进度:\"+\"{:^3.4f}%\"+\"#\"*2).format(i,i/4738*100),end=\"\")\n",
    "    temp_url = \"/bookmanage/wordplus/010010101/{}.txt\".format(str(i))\n",
    "    temp_data = get_text_from_url(temp_url)\n",
    "    temp_f2 = open('./data/word_{}.json'.format(str(i)),'w',encoding='utf-8')\n",
    "    temp_f2.write(temp_data)\n",
    "    temp_f2.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "pleasant-printing",
   "metadata": {},
   "outputs": [],
   "source": [
    "# https://ksedusit-1302986692.file.myqcloud.com/bookmanage/book/chapter/chaptername_010010201.txt\n",
    "# ?sign=1b51b7450cd0c0bd3f6ff6eaa5ac9335&t=1612699280\n",
    "\n",
    "url = \"/bookmanage/book/chapter/chaptername_010010101.txt\"\n",
    "data = get_text_from_url(url)\n",
    "\n",
    "f2 = open('chaptername.json','w',encoding='utf-8')\n",
    "f2.write(data)\n",
    "f2.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dressed-panel",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
